Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace Data
	''' <summary>
	''' Strongly-typed collection for the MemberInfo class.
	''' </summary>
	<Serializable> _
	Public Partial Class MemberInfoCollection 
	Inherits ActiveList(Of MemberInfo, MemberInfoCollection)
	    Public Sub New()
		End Sub
	End Class
	''' <summary>
	''' This is an ActiveRecord class which wraps the MemberInfo table.
	''' </summary>
	<Serializable> _
	Public Partial Class MemberInfo 
	Inherits ActiveRecord(Of MemberInfo)
		#Region ".ctors and Default Settings"
		
		Public Sub New()
			SetSQLProps()
			InitSetDefaults()
			MarkNew()
		End Sub
		
		Public Sub New(ByVal useDatabaseDefaults As Boolean)
			SetSQLProps()
			If useDatabaseDefaults = True Then
				ForceDefaults()
			End If
			MarkNew()
		End Sub
		Public Sub New(ByVal keyID As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByKey(keyID)
		End Sub
		Public Sub New(ByVal columnName As String, ByVal columnValue As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByParam(columnName,columnValue)
		End Sub
		Private Sub InitSetDefaults()
			SetDefaults()
		End Sub
		
		Protected Shared Sub SetSQLProps()
			GetTableSchema()
		End Sub
		#End Region
		
		#Region "Schema and Query Accessor"
		
		Public Shared ReadOnly Property Schema() As TableSchema.Table
			Get
				If BaseSchema Is Nothing Then
					SetSQLProps()
				End If
				Return BaseSchema
			End Get
		End Property
		Private Shared Sub GetTableSchema()
			If (Not IsSchemaInitialized) Then
				'Schema declaration
				Dim schema As TableSchema.Table = New TableSchema.Table("MemberInfo", TableType.Table, DataService.GetInstance("ClubStarterKitData"))
				schema.Columns = New TableSchema.TableColumnCollection()
				schema.SchemaName = "dbo"
				'columns
				
                
                Dim colvarAddress As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarAddress.ColumnName = "address"
                colvarAddress.DataType = DbType.String
                colvarAddress.MaxLength = 500
                colvarAddress.AutoIncrement = false
                colvarAddress.IsNullable = true
                colvarAddress.IsPrimaryKey = false
                colvarAddress.IsForeignKey = false
                colvarAddress.IsReadOnly = false
                
                
                schema.Columns.Add(colvarAddress)
                
                Dim colvarPhone As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarPhone.ColumnName = "phone"
                colvarPhone.DataType = DbType.String
                colvarPhone.MaxLength = 50
                colvarPhone.AutoIncrement = false
                colvarPhone.IsNullable = true
                colvarPhone.IsPrimaryKey = false
                colvarPhone.IsForeignKey = false
                colvarPhone.IsReadOnly = false
                
                
                schema.Columns.Add(colvarPhone)
                
                Dim colvarFirstname As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarFirstname.ColumnName = "firstname"
                colvarFirstname.DataType = DbType.String
                colvarFirstname.MaxLength = 50
                colvarFirstname.AutoIncrement = false
                colvarFirstname.IsNullable = true
                colvarFirstname.IsPrimaryKey = false
                colvarFirstname.IsForeignKey = false
                colvarFirstname.IsReadOnly = false
                
                
                schema.Columns.Add(colvarFirstname)
                
                Dim colvarLastname As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLastname.ColumnName = "lastname"
                colvarLastname.DataType = DbType.String
                colvarLastname.MaxLength = 50
                colvarLastname.AutoIncrement = false
                colvarLastname.IsNullable = true
                colvarLastname.IsPrimaryKey = false
                colvarLastname.IsForeignKey = false
                colvarLastname.IsReadOnly = false
                
                
                schema.Columns.Add(colvarLastname)
                
                Dim colvarAvatar As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarAvatar.ColumnName = "avatar"
                colvarAvatar.DataType = DbType.Binary
                colvarAvatar.MaxLength = 2147483647
                colvarAvatar.AutoIncrement = false
                colvarAvatar.IsNullable = true
                colvarAvatar.IsPrimaryKey = false
                colvarAvatar.IsForeignKey = false
                colvarAvatar.IsReadOnly = false
                
                
                schema.Columns.Add(colvarAvatar)
                
                Dim colvarMemberid As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarMemberid.ColumnName = "memberid"
                colvarMemberid.DataType = DbType.Guid
                colvarMemberid.MaxLength = 0
                colvarMemberid.AutoIncrement = false
                colvarMemberid.IsNullable = false
                colvarMemberid.IsPrimaryKey = true
                colvarMemberid.IsForeignKey = false
                colvarMemberid.IsReadOnly = false
                
                
                schema.Columns.Add(colvarMemberid)
                
                Dim colvarNewsletter As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarNewsletter.ColumnName = "newsletter"
                colvarNewsletter.DataType = DbType.Boolean
                colvarNewsletter.MaxLength = 0
                colvarNewsletter.AutoIncrement = false
                colvarNewsletter.IsNullable = true
                colvarNewsletter.IsPrimaryKey = false
                colvarNewsletter.IsForeignKey = false
                colvarNewsletter.IsReadOnly = false
                
                
                schema.Columns.Add(colvarNewsletter)
                
				BaseSchema = schema
				
				'add this schema to the provider
                'so we can query it later
                DataService.Providers("ClubStarterKitData").AddSchema("MemberInfo",schema)
			End If
		End Sub
		Public Shared Function CreateQuery() As Query
			Return New Query(Schema)
		End Function
		
		#End Region
		
		#Region "Props"
	
          
        <XmlAttribute("Address")> _
        Public Property Address As String 
			Get
				Return GetColumnValue(Of String)("address")
			End Get
		    
			Set
				SetColumnValue("address", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Phone")> _
        Public Property Phone As String 
			Get
				Return GetColumnValue(Of String)("phone")
			End Get
		    
			Set
				SetColumnValue("phone", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Firstname")> _
        Public Property Firstname As String 
			Get
				Return GetColumnValue(Of String)("firstname")
			End Get
		    
			Set
				SetColumnValue("firstname", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Lastname")> _
        Public Property Lastname As String 
			Get
				Return GetColumnValue(Of String)("lastname")
			End Get
		    
			Set
				SetColumnValue("lastname", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Avatar")> _
        Public Property Avatar As Byte() 
			Get
				Return GetColumnValue(Of Byte())("avatar")
			End Get
		    
			Set
				SetColumnValue("avatar", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Memberid")> _
        Public Property Memberid As Guid 
			Get
				Return GetColumnValue(Of Guid)("memberid")
			End Get
		    
			Set
				SetColumnValue("memberid", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Newsletter")> _
        Public Property Newsletter As Nullable(Of Boolean) 
			Get
				Return GetColumnValue(Of Nullable(Of Boolean))("newsletter")
			End Get
		    
			Set
				SetColumnValue("newsletter", Value)
			End Set
		End Property
		
		
		
		
		#End Region
		
		
			    
	    
	    #Region "PrimaryKey Methods"
	    
			Public Function MembersThreadsRecords() As Data.MembersThreadsCollection 
	
				Return New Data.MembersThreadsCollection().Where(MembersThreads.Columns.Memberid, Memberid).Load()
	
			End Function
			
		#End Region
		
		
		
		
		
		
	    
	    'no foreign key tables defined (0)
	    
		
	    
	    #Region "Many To Many Helpers"
	    
	     
        Public Function GetThreadsCollection() As Data.ThreadsCollection 
	        Return MemberInfo.GetThreadsCollection(Me.Memberid)
		End Function
		
        Public Shared Function GetThreadsCollection(ByVal varMemberid As Guid) As Data.ThreadsCollection
        
            Dim cmd As SubSonic.QueryCommand = New SubSonic.QueryCommand("SELECT * FROM Threads INNER JOIN MembersThreads ON Threads.threadid=MembersThreads.threadid WHERE MembersThreads.memberid=@memberid", MemberInfo.Schema.Provider.Name)
            
            cmd.AddParameter("@memberid", varMemberid, DbType.Guid)
            Dim rdr As IDataReader = SubSonic.DataService.GetReader(cmd)
            Dim coll As ThreadsCollection = New ThreadsCollection()
            coll.LoadAndCloseReader(rdr)
            Return coll
            
        End Function
        
        Public Shared Sub SaveThreadsMap(ByVal varMemberid As Guid, ByVal items As ThreadsCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE memberid=@memberid", MemberInfo.Schema.Provider.Name)
            cmdDel.AddParameter("@memberid", varMemberid)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            For Each item As Threads In items 
				Dim varMembersThreads As MembersThreads = New MembersThreads()
				varMembersThreads.SetColumnValue("memberid", varMemberid)
				varMembersThreads.SetColumnValue("threadid", item.GetPrimaryKeyValue())
				varMembersThreads.Save()
			Next
			
        End Sub
        
        
        Public Shared Sub SaveThreadsMap(ByVal varMemberid As Guid, ByVal itemList As System.Web.UI.WebControls.ListItemCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE memberid=@memberid", MemberInfo.Schema.Provider.Name)
            cmdDel.AddParameter("@memberid", varMemberid)
            'add this in
            coll.Add(cmdDel)
            DataService.ExecuteTransaction(coll)
            For Each l As System.Web.UI.WebControls.ListItem In itemList 
            
                If l.Selected
				    
					Dim varMembersThreads As MembersThreads = New MembersThreads()
					varMembersThreads.SetColumnValue("memberid", varMemberid)
					varMembersThreads.SetColumnValue("threadid", l.Value)
					varMembersThreads.Save()
				    
                End If
            Next
        End Sub
        
        Public Shared Sub SaveThreadsMap(ByVal varMemberid As Guid, ByVal itemList() As Guid) 
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE memberid=@memberid", MemberInfo.Schema.Provider.Name)
            cmdDel.AddParameter("@memberid", varMemberid)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            
            For Each item As Guid In itemList
            
                	Dim varMembersThreads As MembersThreads = New MembersThreads()
					varMembersThreads.SetColumnValue("memberid", varMemberid)
					varMembersThreads.SetColumnValue("threadid", item)
					varMembersThreads.Save()
            Next
            
        End Sub
        
		Public Shared Sub DeleteThreadsMap(ByVal varMemberid As Guid) 
		
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE memberid=@memberid", MemberInfo.Schema.Provider.Name)
            cmdDel.AddParameter("@memberid", varMemberid)
            DataService.ExecuteQuery(cmdDel)
        End Sub
	      
	      
	      
	    
	    #End Region
	    
		
		#Region "ObjectDataSource support"
		
		''' <summary>
		''' Inserts a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Insert(ByVal varAddress As String,ByVal varPhone As String,ByVal varFirstname As String,ByVal varLastname As String,ByVal varAvatar As Byte(),ByVal varMemberid As Guid,ByVal varNewsletter As Nullable(Of Boolean))
			Dim item As MemberInfo = New MemberInfo()
			
            item.Address = varAddress
            
            item.Phone = varPhone
            
            item.Firstname = varFirstname
            
            item.Lastname = varLastname
            
            item.Avatar = varAvatar
            
            item.Memberid = varMemberid
            
            item.Newsletter = varNewsletter
            
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		''' <summary>
		''' Updates a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Update(ByVal varAddress As String,ByVal varPhone As String,ByVal varFirstname As String,ByVal varLastname As String,ByVal varAvatar As Byte(),ByVal varMemberid As Guid,ByVal varNewsletter As Nullable(Of Boolean))
			Dim item As MemberInfo = New MemberInfo()
		    
                item.Address = varAddress
				
                item.Phone = varPhone
				
                item.Firstname = varFirstname
				
                item.Lastname = varLastname
				
                item.Avatar = varAvatar
				
                item.Memberid = varMemberid
				
                item.Newsletter = varNewsletter
				
			item.IsNew = False
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		#End Region
		#Region "Columns Struct"
		Public Structure Columns
			Dim x as Integer
			
            Public Shared Address As String = "address"
            
            Public Shared Phone As String = "phone"
            
            Public Shared Firstname As String = "firstname"
            
            Public Shared Lastname As String = "lastname"
            
            Public Shared Avatar As String = "avatar"
            
            Public Shared Memberid As String = "memberid"
            
            Public Shared Newsletter As String = "newsletter"
            
		End Structure
		#End Region
	End Class
End Namespace
